package com.xccwk.serviceImp;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.xccwk.DAO.MybatisDao;
import com.xccwk.mapper.UserMapper;
import com.xccwk.model.User;
import com.xccwk.service.MybatisService;

public class MybatisMapperServiceImp implements MybatisService {

	private SqlSessionFactory sessionFactory = MybatisDao.getInstance();
	//创建能执行映射文件中sql的sqlSession
	

	public List<User> findAll() {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		return userMapper.findAll();
	}

	public List<User> findUserById(int id) {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		return userMapper.findUserById(id);
	}

	public int insert(User user) {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		int i= userMapper.insert(user);
		session.commit();
		session.close();
		return i;
	}

	public int delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		int temp=userMapper.delete(id);
		session.commit();
		session.close();
		return temp;
	}

	public List<User> avageSelect(String like) {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		List<User> listUser=userMapper.avageSelect(like);
		System.out.println(listUser);
		return listUser;
	}

	public Integer selectId(Integer id) {
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		return userMapper.selectId(id.longValue());
	}

	

}
